package com.ht.api.db.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

import cn.hutool.json.JSONNull;

/**
 * 自定义序列化器
 * @author asq
 * @createTime 2025年1月5日 10:29:54
 */
@Configuration
public class JacksonConfig {
    
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        
        // 注册 JSONNull 的序列化器
        SimpleModule module = new SimpleModule();
        module.addSerializer(JSONNull.class, new JsonSerializer<JSONNull>() {
            @Override
            public void serialize(JSONNull value, JsonGenerator gen, 
                    SerializerProvider serializers) throws IOException {
                gen.writeNull();
            }
        });
        objectMapper.registerModule(module);
        
        // 其他配置
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        
        return objectMapper;
    }
}
